﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Nissen_Common;
using Nissen_Dao;
using Nissen_Model;
using Nissen_Resources;
using PosHacker;
using PosHacker.Form;
using PosHacker.PosException;

namespace Nissen_Manage.Forms
{
	public partial class SubjectCourseManage : Form
	{
		private bool _closeFlag = false;
		//private string TestId { get; set; }
		private Test Test { get; set; }
		public POSHacker vPOSHacker { get; set; }
		private List<FormSetCourse> MarkableList { get; set; }
		private DataGridViewCheckBoxHeaderCellHelper _checkBoxHeaderCellHelper;
		private IList<string> selectSubjectCourseIDAndPage = new List<string>();
		public SubjectCourseManage(Test test, POSHacker poshacker)
		{
			InitializeComponent();

			Test = test;
			vPOSHacker = poshacker;
			_checkBoxHeaderCellHelper = new DataGridViewCheckBoxHeaderCellHelper(cbx);
			_checkBoxHeaderCellHelper.AddChekBoxHeaderForGridViewColumn();
			
		}

		private void SubjectCourseManage_Load(object sender, EventArgs e)
		{
			try
			{
				_closeFlag = true;
				this.lblText.Text = Test.TestName;

				SetLoadingPic(true);
				LoadDBData();
				Thread t = new Thread(delegate() { Init(Test); });

				t.Start();
                FormAnswerPaperSelect formtest = new FormAnswerPaperSelect();
                formtest.Show();
			}
			catch (Exception)
			{

				MessageBox.Show(ResourceCommon.GetResource("LoadError"));
			}
		}

		private void LoadDBData()
		{

			SubjectCourseManageService subjectCourseManageService = new SubjectCourseManageService();
			IList<SubjectCourse> dbSubjectCourse = subjectCourseManageService.GetDBSubjectCourse(Test.TestId);

			foreach (var subjectCourse in dbSubjectCourse)
			{
				selectSubjectCourseIDAndPage.Add(subjectCourse.SubjectCourseID + "-" + subjectCourse.SubjectCoursePage);
			}
			BindingDgvDBSubjectCourse(dbSubjectCourse);
		}

		private void Init(Test test)
		{

			try
			{
				//this.vPOSHacker.Login();

				this.GetSubjectCourseNames(test);
			}
			catch (PosException e)
			{
				//this.Invoke();
				MessageBox.Show(e.Message);
			}

		}

		private void GetSubjectCourseNames(Test test)
		{

			MarkableList = this.vPOSHacker.GetMarkableList(test.TestId);

			this.Invoke(new PosDelegate(SetSubjectCourses));
		}
		private void SetSubjectCourses()
		{


			IList<SubjectCourse> subjectCourses = new List<SubjectCourse>();
			foreach (var model in MarkableList)
			{
				SubjectCourse subjectCourse = new SubjectCourse();
				subjectCourse.SubjectCourseID = model.subjectCourseTestId;
				subjectCourse.SubjectCourseName = model.subjectCourseNm;
				subjectCourse.SubjectCoursePage = model.pageNum;
				subjectCourse.SubjectCourseSheetNum = model.SheetsNum;
				subjectCourse.Test = Test;

				if (selectSubjectCourseIDAndPage.Contains(model.subjectCourseTestId + "-" + model.pageNum))
				{
					subjectCourse.DisplayFlag = true;
				}
				subjectCourses.Add(subjectCourse);
			}

			BindingDgvSubjectCourse(subjectCourses);

			_closeFlag = false;
			SetLoadingPic(false);
		}


		private void BindingDgvSubjectCourse(IList<SubjectCourse> subjectCourses)
		{

			this.dgvSubjectCourse.AutoGenerateColumns = false;
			this.dgvSubjectCourse.DataSource = subjectCourses;

		}
		private void BindingDgvDBSubjectCourse(IList<SubjectCourse> subjectCourses)
		{

			this.dgvDBSubjectCourse.AutoGenerateColumns = false;
			this.dgvDBSubjectCourse.DataSource = subjectCourses;

		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			try
			{
				IList<SubjectCourse> subjectCourses = GetSelectSubjectCourse();

				SubjectCourseManageService subjectCourseManageService = new SubjectCourseManageService();

				subjectCourseManageService.SaveSubjectCourse(subjectCourses,Test);

				MessageBox.Show(ResourceCommon.GetResource("SaveSuccess"));
				LoadDBData();
			}
			catch (Exception ex)
			{

				MessageBox.Show(ResourceCommon.GetResource("SaveFail"));
				//MessageBox.Show(ex.Message);
			}
			
		}

		/// <summary>
		/// 获取选择数据
		/// </summary>
		/// <returns></returns>
		private IList<SubjectCourse> GetSelectSubjectCourse()
		{
			IList<SubjectCourse> subjectCourses = new List<SubjectCourse>();

			for (int i = 0; i < dgvSubjectCourse.Rows.Count; i++)
			{

				if (dgvSubjectCourse.Rows[i].Cells[0].Value != null)
				{
					if (dgvSubjectCourse.Rows[i].Cells[0].Value.ToString() == "True")
					{
						SubjectCourse subjectCourse = (SubjectCourse)dgvSubjectCourse.Rows[i].DataBoundItem;
						
						subjectCourses.Add(subjectCourse);
					}
				}
			}
			return subjectCourses;
		}


		private void SubjectCourseManage_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (_closeFlag)
			{
				e.Cancel = true;
				MessageBox.Show(ResourceCommon.GetResource("LoadingNotClose"));
			}
			
		}

	

		private void dgvSubjectCourse_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.RowIndex != -1)
			{
				SubjectCourse subjectCourse = (SubjectCourse)dgvSubjectCourse.Rows[e.RowIndex].DataBoundItem;
				if (subjectCourse != null)
				{
					var tempMark = MarkableList[e.RowIndex];
					tempMark.reservationPriority = "1";
					tempMark.testExecId = this.Test.TestId;
					MarkableQuestionsManage markableQuestionsManage = new MarkableQuestionsManage(tempMark, this.vPOSHacker);
					markableQuestionsManage.ShowDialog();
				}
			}
		}
		private void SetLoadingPic(bool flag)
		{
			this.pictureBox1.Visible = flag;
		}
	}
}
